package com.rm.freedrawview;

import ohos.agp.render.Paint;
import ohos.agp.render.PathEffect;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.List;

/**
 * Created by Riccardo Moro on 10/23/2016.
 *
 * @author author
 * @version 1.0.0
 */
public class FreeDrawHelper {
    /**
     * Function used to check whenever a list of points is a line or a path to draw
     *
     * @param points points
     * @return boolean
     */
    static boolean isAPoint(List<Point> points) {
        if (points.size() == 0) {
            return false;
        }

        if (points.size() == 1) {
            return true;
        }

        for (int i = 1; i < points.size(); i++) {
            if (points.get(i - 1).x != points.get(i).x || points.get(i - 1).y != points.get(i).y) {
                return false;
            }
        }
        return true;
    }

    /**
     * Create, initialize and setup a paint
     *
     * @param paintAlpha paintAlpha
     * @param paintColor paintColor
     * @param fill       fill
     * @param paintWidth paintWidth
     * @return Paint
     */
    static Paint createPaintAndInitialize(
            int paintColor, int paintAlpha,
            float paintWidth, boolean fill) {
        Paint paint = createPaint();
        initializePaint(paint, paintColor, paintAlpha, paintWidth, fill);
        return paint;
    }

    static Paint createPaint() {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        return paint;
    }

    static void initializePaint(
            Paint paint, int paintColor,
            int paintAlpha, float paintWidth,
            boolean fill) {
        if (fill) {
            setupFillPaint(paint);
        } else {
            setupStrokePaint(paint);
        }
        paint.setStrokeWidth(paintWidth);
        paint.setColor(new Color(paintColor));
        paint.setAlpha(paintAlpha / 255f);
    }

    static void setupFillPaint(Paint paint) {
        paint.setStyle(Paint.Style.FILL_STYLE);
    }

    static void setupStrokePaint(Paint paint) {
        paint.setStrokeJoin(Paint.Join.ROUND_JOIN);
        paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        paint.setPathEffect(new PathEffect(
                new PathEffect(100f),
                new PathEffect(100f)));
        paint.setStyle(Paint.Style.STROKE_STYLE);
    }

    static void copyFromPaint(Paint from, Paint to, boolean copyWidth) {

        to.setColor(from.getColor());
        to.setAlpha(from.getAlpha());

        if (copyWidth) {
            to.setStrokeWidth(from.getStrokeWidth());
        }
    }

    static void copyFromValues(
            Paint to, int color,
            int alpha, float strokeWidth,
            boolean copyWidth) {
        to.setColor(new Color(color));
        to.setAlpha(alpha / 255f);

        if (copyWidth) {
            to.setStrokeWidth(strokeWidth);
        }
    }

    /**
     * Converts a given dp number to it's pixel corresponding number
     *
     * @param context context
     * @param vp      vp
     * @return float
     */
    public static float convertDpToPixels(Context context, float vp) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        float dpi = display.getAttributes().densityPixels;
        return (float) (vp * dpi + 0.5d * (vp >= 0 ? 1 : -1));
    }

    /**
     * Converts a given pixel number to it's dp corresponding number
     *
     * @param context context
     * @param px      px
     * @return int
     */
    public static int convertPixelsToDp(Context context, float px) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        float dpi = display.getAttributes().densityPixels;
        return (int) (px / dpi + 0.5d);
    }
}
